package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmsShop;
import com.macro.mall.portal.service.PmsShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import java.util.List;


@RestController
@Api(tags = "pmsShopController", description = "店铺管理")
@RequestMapping("pmsShop")
public class PmsShopController {
    /**
     * 服务对象
     */
    @Autowired
    private PmsShopService pmsShopService;

    /**
     * 根据主键id查询单个店铺信息
     *
     * @param id 主键
     * @return 实例对象
     */
    @ApiOperation("根据店铺id查询店铺详细信息")
    @GetMapping("selectOne/{id}")
    public CommonResult selectOne(@PathVariable Long id) {
        System.out.println("id => " + id);
        System.out.println("shop => ");
        System.out.print(this.pmsShopService.queryById(id));
        return CommonResult.success(this.pmsShopService.queryById(id));
    }

    /**
     * 分页查询所有店铺信息
     *
     * @param pageNum 当前第几页
     * @param pageSize 一页有多少记录
     * @return 所有店铺信息list
     */
    @ApiOperation("查询所有店铺")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsShop>> selectAll(@RequestParam(value = "pageSize", defaultValue = "8") Integer pageSize,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        List<PmsShop> pmsShopList = pmsShopService.selectAll(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(pmsShopList));
    }


    /**
     * 组合查询
     *
     * @param pageNum 当前第几页
     * @param pageSize 一页有多少记录
     * @param shopName 店铺名字模糊查询
     * @param status 店铺状态0->不显示；1->显示
     * @return 查询结果列表
     */
    @ApiOperation("根据店铺名字和状态组合模糊查询")
    @GetMapping("selectByNameAndStatus")
    @ResponseBody
    public CommonResult<CommonPage<PmsShop>> selectByNameAndStatus(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(value = "keyword", required = false) String shopName,
                                                                   @RequestParam(value = "status", required = false) String status){
        int showStatus=0;
        if(status.isEmpty()){//判断状态是否为空，因为int无法判断所以接受string类型
            showStatus=-1;
        }else{
            showStatus=Integer.parseInt(status);//判断非空后转换成int类型
        }
        List<PmsShop> pmsShopList = pmsShopService.selectByNameAndStatus(pageSize, pageNum,shopName,showStatus);
        return CommonResult.success(CommonPage.restPage(pmsShopList));
    }
}
